<template>
  <!-- 运营方管理 -->
  <BaseMainSlot :show-tab="true">
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        :has-save="false"
        :has-del="false"
        @add="add"
        @cancel="cancel"
        @del="del"
        @edit="edit"
        @save="save"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">运营方名称:</div>
          <div class="input">
            <el-input v-model="searchForm.name" clearable @keyup.enter.native="search" @blur="search" @clear="search" />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">法人:</div>
          <div class="input">
            <el-input v-model="searchForm.legalPerson" clearable @keyup.enter.native="search" @blur="search" @clear="search" />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">联系人:</div>
          <div class="input">
            <el-input v-model="searchForm.contactor" clearable @keyup.enter.native="search" @blur="search" @clear="search" />
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <!-- 业务组件展示表格 -->
      <div class="zh-table-view">
        <el-table
          :data="tableData"
          height="100%"
          @row-click="handleEdit"
        >
          <el-table-column
            type="index"
            width="50"
          />
          <el-table-column
            prop="name"
            label="运营方名称"
          />
          <el-table-column
            prop="address"
            label="运营方地址"
          />
          <el-table-column
            prop="legalPerson"
            label="法人"
          />
          <el-table-column
            prop="contactor"
            label="联系人"
          />
          <el-table-column
            prop="phoneNumber"
            label="联系电话"
          />
          <el-table-column
            prop="businessLicense"
            label="营业执照"
          >
            <template v-if="scope.row.businessLicense" slot-scope="scope">
              <img :src="'data:image/jpeg;base64,' + scope.row.businessLicense" alt="" style="height: 50px;" />
            </template>
          </el-table-column>
          <el-table-column
            prop="serviceArea"
            label="服务范围"
          />
          <el-table-column
            prop="status"
            label="状态"
          >
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="启用"
                inactive-text="停用" @change="editStatus(scope.row)" active-color="#13ce66" inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="edit"
              >修改</el-button>
              <!-- <el-button
                size="mini"
                type="danger"
                @click="dellineallmes(scope.$index, scope.row)"
              >删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :page-size="pageSize"
          :current-page="currentPage"
          :total-size="totalSize"
          @changePageSize="changePageSize"
          @changeCurrentPage="changeCurrentPage"
        />
      </div>
    </template>
    <template v-slot:dialog>
      <!-- 新增运营方 -->
      <el-dialog
        v-if="dialogFormVisible"
        :title="titleText + '运营方'"
        class="formData"
        :visible.sync="dialogFormVisible"
        width="55%"
        top="5vh"
        :before-close="() => { cancel() }"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="运营方名称:" prop="name" class="w50p">
            <el-input v-model="formData.name" clearable />
          </el-form-item>
          <el-form-item v-if="titleText == '添加'" label="登录名称:" prop="loginName" class="w50p">
            <el-input v-model="formData.loginName" clearable />
          </el-form-item>
          <el-form-item label="运营方地址:" prop="area" class="w50p">
            <el-cascader
              v-model="formData.area"
              :options="addressList"
              @change="areaChange"
              style="width: 100%;"
            />
          </el-form-item>
          <el-form-item v-if="titleText == '添加'" label="登录密码:" prop="loginPassword" class="w50p">
            <el-input v-model="formData.loginPassword" clearable />
          </el-form-item>
          <el-form-item label="详细地址:" prop="address" class="w50p">
            <el-input v-model="formData.address" clearable />
          </el-form-item>
          <el-form-item label="法人:" prop="legalPerson" class="w50p">
            <el-input v-model="formData.legalPerson" clearable />
          </el-form-item>
          <el-form-item label="联系人:" prop="contactor" class="w50p">
            <el-input v-model="formData.contactor" clearable />
          </el-form-item>
          <el-form-item label="联系电话:" prop="phoneNumber" class="w50p">
            <el-input v-model="formData.phoneNumber" clearable />
          </el-form-item>
          <el-form-item v-if="titleText == '添加'" label="覆盖同步:" prop="forceSyncUserName" class="w50p">
            <el-select v-model="formData.forceSyncUserName" placeholder="请选择">
              <el-option label="是" :value="true"></el-option>
              <el-option label="否" :value="false"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="营业执照:" prop="businessLicense" class="w100p">
            <Upload v-if="dialogFormVisible" :url-list="formData.pictureList" :limit="1" @getFile="getFile" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="save">保 存</el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar/index";
import { mymixin } from "@/utils/mixins";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
// import zhreq from "@/api/base-manage/base-user-manage";
import axios from "axios";
import Pagination from "@/components/Pagination";
import { addressList } from '@/utils/address'
import { socialServiceUrl } from "@/api/baseURL"
import Upload from "./components/upload";
export default {
  components: { ActionBar, BaseMainSlot, Pagination, SearchBox, Upload },
  mixins: [mymixin],
  data() {
    return {
      addressList: addressList,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      totalSize: 0,
      // ---添加相关---
      formData: {},
      databaseName: 'DB_TDW1_PUBLIC',
      searchForm: {
        name: ''
      },
      rules: {
        name: [{ required: true, message: "请输入运营方名称", trigger: "blur" }],
        loginName: [{ required: true, message: "请输入登录名称", trigger: "blur" }],
        loginPassword: [{ required: true, message: "请输入登录密码", trigger: "blur" }],
        area: [{ required: true, message: "请输入运营方地址", trigger: "blur" }],
        legalPerson: [{ required: true, message: "请输入法人", trigger: "blur" }],
        address: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
        contactor: [{ required: true, message: "请输入联系人", trigger: "blur" }],
        phoneNumber: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1\d{10}))$/,
            message: "请输入合法手机号/电话号",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.gettabledataList()
  },
  methods: {
    // 获取表格信息
    async gettabledataList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          name: this.searchForm.name || undefined,
          legalPerson: this.searchForm.legalPerson || undefined,
          contactor: this.searchForm.contactor || undefined
        }
      };
      const { data: mes } = await axios({
        method: "post",
        url: this.$webSerUrlPro + 'organization/socialized/serviceProvider/queryPageByCondition',
        // url: socialServiceUrl + '/service/serviceProvider/queryPageByCondition',
        headers: {
          databaseName: this.databaseName
        },
        data: params
      })
      // console.log(mes)
      if (mes.code === 1) {
        this.tableData = mes.data.content || []
        this.totalSize = mes.data.totalElements
        this.resetActionBar()
      } else {
        this.$message.warning("获取表格失败")
      }
    },
    handleSelectionChange() {
      this.nowselectedList = [];
      const list = this.$refs.catetreetwo.getCheckedNodes();
      this.nowselectedList = list.map((item) => {
        return item.id;
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) {
        this.delActive = true;
      } else this.delActive = false;
    },
    // 搜索
    search() {
      this.currentPage = 1;
      this.gettabledataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.gettabledataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.gettabledataList();
    },
    // 修改
    handleEdit(row) {
      if (row) {
        let area = []
        row.provinceId && area.push(row.provinceId)
        row.cityId && area.push(row.cityId)
        row.areaId && area.push(row.areaId)
        let pictureList = row.businessLicense ? [{ url: 'data:image/jpeg;base64,' + row.businessLicense }] : []
        this.formData = { ...row, area, pictureList };
      }
    },
    // 删除一个业务组件
    dellineallmes(i, e) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios({
          method: 'get',
          url: this.$webSerUrlPro + 'organization/socialized/serviceProvider/delBySocializedServiceProviderNo?no=' + e.serviceProviderNo,
          // url: socialServiceUrl + '/service/serviceProvider/delBySocializedServiceProviderNo?no=' + e.serviceProviderNo,
          headers: {
            databaseName: this.databaseName
          }
        }).then(res => {
          if (res.data.code === 1) {
            this.$message.success("删除成功！")
            this.gettabledataList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 保存要添加的业务的组件信息
    save() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          let param = {
            ...this.formData,
            provinceId: this.formData.area && this.formData.area[0] || undefined,
            cityId: this.formData.area && this.formData.area[1] || undefined,
            areaId: this.formData.area && this.formData.area[2] || undefined
          };
          delete param.area
          delete param.pictureList
          if (this.nowChanceStau === "edit") {
            axios({
              method: "post",
              url: this.$webSerUrlPro + 'organization/socialized/serviceProvider/edit',
              // method: "put",
              // url: socialServiceUrl + '/service/serviceProvider/edit',
              headers: {
                databaseName: this.databaseName
              },
              data: param
            }).then(res => {
              if (res.data.code === 1) {
                this.$message.success("修改成功！")
                this.gettabledataList()
                this.dialogFormVisible = false
              } else {
                this.$message.error(res.data.msg)
              }
            })
          } else if (this.nowChanceStau === "add") {
            axios({
              method: "post",
              url: this.$webSerUrlPro + 'organization/socialized/serviceProvider/add',
              // url: socialServiceUrl + '/service/serviceProvider/add',
              headers: {
                databaseName: this.databaseName
              },
              data: param
            }).then(res => {
              if (res.data.code === 1) {
                this.$message.success("添加成功！")
                this.gettabledataList()
                this.dialogFormVisible = false
              } else {
                this.$message.error(res.data.msg)
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    //修改用户状态
    editStatus (data) {
      this.$confirm('您确定要修改该运营方的状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios({
          method: "post",
          url: this.$webSerUrlPro + 'organization/socialized/serviceProvider/edit',
          // method: "put",
          // url: socialServiceUrl + '/service/serviceProvider/edit',
          headers: {
            databaseName: this.databaseName
          },
          data: {
            id: data.id,
            serviceProviderNo: data.serviceProviderNo,
            status: data.status
          }
        }).then(res => {
          if (res.data.code == 1) {
            this.gettabledataList()
            this.$message({
              type: 'success',
              message: '修改成功!'
            });
          } else {
            data.status = data.status == 1 ? 0 : 1
            this.$message({
              type: 'info',
              message: res.data.msg
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    // 上传照片
    getFile(file) {
      this.handleImg(file, 'businessLicense')
    },
    handleImg(file, field) {
      let base = file.length ? file[0].url.split('data:image/jpg;base64,')[1] : ''
      this.$set(this.formData, field, base)
      this.$refs.formData.validateField([field], () => {})
    },
    areaChange () {
      let address = this.getAdress(this.formData.area)
      axios({
        method: "get",
        url: `https://restapi.amap.com/v3/geocode/geo?key=8187d1c02d1ba1cd0e808bce16674652&address=${address}`,
      }).then(res => {
        let data = res.data.geocodes[0]
        let location = data && data.location && data.location.split(',')
        this.$set(this.formData, 'longitude', location[0])
        this.$set(this.formData, 'latitude', location[1])
      })
    },
    getAdress (list) {
      let provinceId = list[0], cityId = list[1], areaId = list[2]
      let row = addressList.find(el => { return el.value == provinceId })
      let row2 = row && row.children.find(el => { return el.value == cityId })
      let row3 = row2 && row2.children.find(el => { return el.value == areaId })
      return row.label + row2.label + row3.label
    },
  }
};
</script>

<style lang="scss" scoped>
  @import "~@/styles/variables.scss";

  .business-main{
  width: 100%;
  height: 100%;
  .topbar-view{
    width: 100%;
    margin-bottom: 5px;
  }
  .zh-table-view{
    width: 100%;
    height: calc(100% - 60px);
    position: relative;
  }
}
.add-components-dia {
  width: 502px;
  // height: 191px;
  background: #303640;
  border: 1px solid #005c6f;
  border-radius: 7px;
  position: fixed;
  top: 15%;
  left: 40%;
  // background-color: #005c6f;
  z-index: 99;
  .top-bar {
    width: 502px;
    height: 10px;
    background: linear-gradient(90deg, #3581fb 0%, #00faad 100%);
    border-radius: 7px 7px 0px 0px;
  }
  .top-title-line {
    width: 490px;
    height: 35px;
    line-height: 35px;
    padding: 6px 8px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #484a4e;
    .line-title {
      height: 20px;
      font-size: 14px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: $textColor;
      letter-spacing: 2px;
    }
    .line-close {
      color: #10d7c4;
      cursor: pointer;
    }
    .line-close:hover {
      color: $textColor;
    }
  }
  .screen-dia-main {
    width: 502px;
    padding: 20px 10px;
    // max-height: 550px;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 480px;
    .title-config{
      padding-bottom: 20px;
      border-bottom: #00faad 1px solid;
      margin-bottom: 10px;
    }
    .dia-main-inputline {
      width: 482px;
      margin-top: 5px;
      display: flex;
      // justify-content: center;
      align-items: center;
      margin-bottom: 10px;
      .inputline-name {
        width: 120px;
        height: 24px;
        font-size: 16px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: $textColor;
        line-height: 24px;
        text-align: center;
      }
      .inputline-value {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }
      .textarealine-value {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }
      .icon-line-value {
        width: 48px;
        height: 48px;
        text-align: center;
        .not-iconurl {
          border: 1px dashed #d9d9d9;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
        }
        .not-iconurl:hover {
          border-color: #1caf7e;
        }
        .not-iconsty {
          font-size: 20px;
          color: #8c939d;
          width: 48px;
          height: 48px;
          line-height: 48px;
          text-align: center;
        }
      }
      .selcect-one {
        width: 350px;
        height: 32px;
        box-sizing: border-box;
        border-radius: 0px 0px 0px 0px;
        opacity: 1;
        border: 1px solid #494e57;
        box-sizing: border-box;
        outline: 0;
      }
    }
    .dia-main-addline {
      width: 442px;
      font-size: 16px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: $textColor;
      line-height: 24px;
      border-bottom: #00faad 1px dashed;
      padding-bottom: 16px;
      margin-bottom: 10px;
      .components-line {
        display: flex;
        // justify-content: space-between;
        align-items: center;
        width: 442px;
        height: 48px;
        .slect-name {
          width: 90px;
          height: 36px;
          font-size: 16px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: $textColor;
          line-height: 36px;
          text-align: center;
        }
        .selcect-one {
          width: 350px;
          color: #00faad;
          margin-top: 12px;
          height: 36px;
          line-height: 36px;
          background-color: transparent;
          border: #00faad dashed 1px;
          cursor: pointer;
          option {
            background-color: #005c6f !important;
            color: $textColor;
            cursor: pointer;
          }
        }
      }
      .addline-but {
        width: 442px;
        display: flex;
        justify-content: space-around;
        margin-top: 15px;
        .addline-but1 {
          color: #00faad;
          border: 1px solid #00faad;
          cursor: pointer;
          padding: 5px 10px;
        }
        .addline-but1:hover {
          color: $textColor;
        }
        .addline-but2 {
          color: #3581fb;
          border: 1px solid #3581fb;
          cursor: pointer;
          padding: 5px 10px;
        }
        .addline-but2:hover {
          color: $textColor;
        }
      }
    }
    .dia-main-addline:last-of-type {
      padding-bottom: 0;
      margin: 0;
      border: none;
    }
    .showkuang-one {
      position: relative;
      left: 41px;
      width: 400px;
      min-height: 200px;
      .kuangone-main {
        position: absolute;
        width: 100%;
        height: 400px;
      }
    }
  }
  .bottom-bar {
    width: 482px;
    height: 50px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    font-size: 16px;
    .bottom-bar-cancel {
      width: 96px;
      height: 33px;
      border-radius: 5px 5px 5px 5px;
      border: 1px solid #0cb9ae;
      color: #0cb9ae;
      cursor: pointer;
      line-height: 33px;
      text-align: center;
    }
    .bottom-bar-cancel:hover {
      color: #00faad;
    }
    .bottom-bar-confirm {
      width: 96px;
      height: 33px;
      background: #0cb9ae;
      border-radius: 5px 5px 5px 5px;
      cursor: pointer;
      line-height: 33px;
      text-align: center;
    }
    .bottom-bar-confirm:hover {
      color: #10fab4;
    }
  }
}
::v-deep .el-switch__label {
  color: #fff !important;

  &.is-active {
    color: #409EFF !important;
  }
}
::v-deep .el-dialog {
  min-width: 35% !important;
}
.search-list {
  .common-search-item {
    .label {
      min-width: 80px !important;
      text-align: right;
    }
  }
}
</style>
